package com.tysong.qq.listener;

import com.google.common.collect.Lists;
import com.tysong.qq.ApiInstance;
import com.tysong.qq.patterns.Context;
import com.tysong.qq.patterns.Strategy;
import com.tysong.qq.patterns.impl.OperationClockIn;
import com.tysong.qq.patterns.impl.OperationGetCheckList;
import com.tysong.qq.service.*;
import com.tysong.qq.utils.Compared;
import love.forte.common.ioc.annotation.Beans;
import love.forte.simbot.annotation.Filter;
import love.forte.simbot.annotation.Listen;
import love.forte.simbot.api.message.events.GroupMemberIncrease;
import love.forte.simbot.api.message.events.GroupMsg;
import love.forte.simbot.api.message.events.PrivateMsg;
import love.forte.simbot.api.sender.MsgSender;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.*;
import java.util.stream.Collectors;

@Beans
public class GroupListener {

    @Autowired
    private Map<String, Strategy> strategyMap;
    String a = "";

    //只处理哪些qq群
    private static final List<String> processGroupNumber = Lists.newArrayList();
    // 这里是总名单人数，可用yml配置和云配置
    // private static final String[] INITIAL_DATA_LIST = {"表情包老铁", "张三", "李四", "王五"};
    List<String> listName = new ArrayList<>();

    static {
        processGroupNumber.add("874698794");
    }

    /**
     * 私信消息
     */
    @Listen(PrivateMsg.class)
    public void privateMsg(PrivateMsg msg, MsgSender sender) {
        System.out.println("收到消息：" + msg);
    }

    /**
     * 群消息
     * @Filter(at=true) 只要at我的信息
     */
    @Listen(GroupMsg.class)
    @Filter(anyAt = true)
    public void groupMsg(GroupMsg msg, MsgSender sender) throws Exception {

        String groupNumber = msg.getGroupInfo().getGroupCode();
        if (!processGroupNumber.contains(groupNumber)) {
            return;
        }
        if (StringUtils.contains(msg.getMsg(), "天气")) {
            System.out.println("收到消息：" + msg);
            String[] data = msg.getMsg().split(" ");
            String city = data[1].replaceAll("天气", "");
            String weather = TianQiService.INSTANCE.getWeather(city);
            sender.SENDER.sendGroupMsg(groupNumber, weather);
            return;
        }
        if (StringUtils.contains(msg.getMsg(), "图片")) {
            String msg1 = TuPianService.INSTANCE.msg(ApiInstance.imgApi);
            sender.SENDER.sendGroupMsg(groupNumber, msg1);
            return;
        }
        if (StringUtils.contains(msg.getMsg(), "夸我")) {
            System.out.println("收到消息：" + msg);
            String msg1 = CaiHongPiService.INSTANCE.caihongpi("");
            sender.SENDER.sendGroupMsg(groupNumber, msg1);
            return;
        }
        if (StringUtils.contains(msg.getMsg(), "打卡")) {
            clockIn(msg, sender, groupNumber, listName);
            return;
        }
        if (StringUtils.contains(msg.getMsg(), "名单")) {
            getCheckList(sender, groupNumber, listName);
            return;
        }
        if (StringUtils.contains(msg.getMsg(), "下载")) {
            String[] split = msg.getMsg().split(":");
            String musicName = split[2];
            getMusic(sender, groupNumber, musicName);
        }
        if (StringUtils.contains(msg.getMsg(), "功能") || StringUtils.contains(msg.getMsg(), "菜单")) {
            String outText = "当前助手支持功能:" + "\n"
                    + "1、天气查询:北京天气"
                    + "2、成语接龙:一马当先"
                    + "3、图片:图片"
                    + "4、打卡：打卡"
                    + "5、名单：名单";
            sender.SENDER.sendGroupMsg(groupNumber, outText);
            return;
        }
        String[] data = msg.getMsg().split(" ");
        String chengyu = data[1];
        if (chengyu.length() == 4) {
            String jieLong = ChengyuService.INSTANCE.jieLong(chengyu);
            sender.SENDER.sendGroupMsg(groupNumber, jieLong);
        }
    }

    /**
     * 网易云点歌
     */
    private void getMusic(MsgSender sender, String groupNumber, String musicName) throws Exception {
        Context context = new Context(new OperationClockIn());
        context.getMusicName(sender, groupNumber, musicName);
    }

    /**
     * 进行打卡功能
     */
    private void clockIn(GroupMsg msg, MsgSender sender, String groupNumber, List<String> listName) {
        Context context = new Context(new OperationClockIn());
        context.executeStrategy(msg, sender, groupNumber, listName);
    }

    /**
     * 获取打卡名单那
     */
    private void getCheckList(MsgSender sender, String groupNumber, List<String> listName) {
        Context context = new Context(new OperationGetCheckList());
        context.executeStrategy(null, sender, groupNumber, listName);
    }

    @Listen(GroupMemberIncrease.class)
    public void groupMemAdd(GroupMemberIncrease groupMemberIncrease, MsgSender sender) {
        String groupNumber = groupMemberIncrease.getGroupInfo().getGroupCode();
        if (!processGroupNumber.contains(groupNumber)) {
            return;
        }
        String beOperatedQQ = groupMemberIncrease.getBeOperatorInfo().getBeOperatorCode();
        sender.SENDER.sendGroupMsg(groupNumber, "欢迎" + beOperatedQQ + "加入本群");
    }

    /**
     * 定时清理List
     */
    @Scheduled(cron = "23 58 01 * * ?")
    public void schedulerTask() {
        listName.clear();
    }

    public static void main(String[] args) {
        String[] initialDataList = {"表情包老铁", "张三", "李四", "王五"};
        String[] strNewName = {"表情包老铁", "张三", "王五"};
        List<String> checkIsList = new ArrayList<>(Arrays.asList(initialDataList));
        List<String> noCheckIsList = new ArrayList<>(Arrays.asList(strNewName));
        List<String> different = Compared.getDifferent(checkIsList, noCheckIsList);
        different.forEach(System.out::println);

    }
}
